﻿using LogHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KinTN_WMS_CL.Extensions
{
    public class LogServices: ILogServices
    {
        private static object root = new object();
        public  void Log(string message)
        {
            lock (root)
            {
                string dir = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Log");
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                string file = Path.Combine(dir, string.Format("{0}.log", DateTime.Now.Date.ToString("yyyy-MM-dd")));
                using (StreamWriter sw = new StreamWriter(file, true))
                {
                    sw.WriteLine(message);
                }
            }
        }
        public  void DeleteLog()
        {

            try
            {
                string folderPath = AppDomain.CurrentDomain.BaseDirectory + "Log";
                DirectoryInfo dir = new DirectoryInfo(folderPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo) //判断是否文件夹
                    {
                        DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                        subdir.Delete(true); //删除子目录和文件
                    }
                    else
                    {

                        string[] Name = i.Name.Split('.');
                        if (Name.Length == 2)
                        {
                            DateTime Otime = Convert.ToDateTime(Name[0]);
                            if (Otime < DateTime.Now.AddDays(-20))
                            {
                                File.Delete(i.FullName); //删除指定文件
                            }

                        }
                    }
                }
            }
            catch (Exception)
            {


            }
        }

    }
}
